package ye.guo.huang.easypoidemooo1.easypoidemooo1.against;

import org.apache.poi.ss.usermodel.*;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

public class AgainstTest {

    @Test
    public void test002() throws Exception {
        //分组数
        int groupNum = 4 ;
        //每个组队伍数量
        int teamNum = 4 ;
        //赛程列表的key
        String schedule = "schedule";
        //对阵图的key
        String against = "against";
        //最终的数据结构 key=schedule->赛程列表，key=against=对阵图
        List<Map<String,Object>> resultList = new ArrayList<>();
        //造赛程数据
        for (int g = 0; g < groupNum; g++) {
            List<AgainstVO> scheduleList = new ArrayList<>() ;
            for (int i = 0; i < teamNum; i++) {
                for (int j = i+1; j < teamNum; j++) {
                    Integer number = teamNum*(i-1)+j ;
                    AgainstVO vo = AgainstVO
                            .builder()
                            .number(number)
                            .againstHost("队伍" + i)
                            .againstGuest("队伍" + j)
                            .date("22-1-2" + i)
                            .time("9:00")
                            .location(number+"地点")
                            .scoreHost(i + "")
                            .scoreGuest(j + "")
                            .build();
                    scheduleList.add(vo);
                }
            }
            Map<String,Object> resultMap = new LinkedHashMap<>();
            resultMap.put(schedule,scheduleList);
            resultList.add(resultMap);
        }
        System.out.println(resultList);
        //构建对阵数据
        for (Map<String, Object> resultMap : resultList) {
            List<AgainstVO> scheduleList = (List<AgainstVO>)resultMap.get(schedule);
            List<List<String>> againstList = new ArrayList<>();
            //赛程数
            int len = scheduleList.size();
            //队伍数
            int teamNumTemp = getTeamNum(scheduleList);
            //第一行
            List<String> header = new ArrayList<>();
            header.add("");
            for (int i = 0; i < teamNumTemp; i++) {
                header.add("队伍"+i);
            }
            againstList.add(header);
            for (int i = 0; i < teamNumTemp; i++) {
                List<String> rowTemp = new ArrayList<>();
                String hostTemp = "队伍"+i;
                rowTemp.add(hostTemp);
                for (int j = 0; j < teamNumTemp; j++) {
                    String guestTemp = "队伍"+j;
                    List<AgainstVO> scleduleListTemp = scheduleList.stream().filter(e->e.getAgainstHost().equals(hostTemp)&&e.getAgainstGuest().equals(guestTemp)).collect(Collectors.toList());
                    String score = "";
                    if (scleduleListTemp.size() > 0) {
                        score = scleduleListTemp.get(0).getScoreHost() + " : " + scleduleListTemp.get(0).getScoreGuest();
                    }else{
                        scleduleListTemp = scheduleList.stream().filter(e->e.getAgainstHost().equals(guestTemp)&&e.getAgainstGuest().equals(guestTemp)).collect(Collectors.toList());
                        if (scleduleListTemp.size()>0) {
                            score = scleduleListTemp.get(0).getScoreGuest() + " : " + scleduleListTemp.get(0).getScoreHost();
                        }
                    }
                    rowTemp.add(score);
                }
                againstList.add(rowTemp);
            }
            resultMap.put(against,againstList);
        }
        System.out.println(resultList);

        //画Excel
        String path = "C:\\software\\15_intellij\\IdeaProjects\\easypoi-demooo1\\src\\main\\java\\ye\\guo\\huang\\easypoidemooo1\\easypoidemooo1\\against\\test001.xls";
        String pathFinished = "C:\\software\\15_intellij\\IdeaProjects\\easypoi-demooo1\\src\\main\\java\\ye\\guo\\huang\\easypoidemooo1\\easypoidemooo1\\against\\test00001.xls";
        Workbook workbook = WorkbookFactory.create(new File(path));
        //样式
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        Sheet sheet = workbook.getSheetAt(0);
        for (int i = 0; i < 100; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 20; j++) {
                Cell cell = row.createCell(j);
            }
        }
        //画图所在的行位置  内容从2开始
        int  indexAll = 0 ;
        for (int i = 0; i < resultList.size(); i++) {
            Map<String, Object> resultMap = resultList.get(i);
            List<AgainstVO> scheduleList = (List<AgainstVO>)resultMap.get(schedule);
            List<List<String>> againstList = (List<List<String>>)resultMap.get(against);
            indexAll ++ ;
            sheet.getRow(indexAll++).getCell(0).setCellValue("分组"+i);
            //赛程数
            int nowRowNum = indexAll;
            //画赛程
            for (AgainstVO vo : scheduleList) {
                Row row = sheet.getRow(indexAll++);
                row.getCell(0).setCellValue(vo.getAgainstHost()+"-"+vo.getAgainstGuest());
                row.getCell(1).setCellValue(vo.getDate());
                row.getCell(2).setCellValue(vo.getTime());
                row.getCell(3).setCellValue(vo.getLocation());

                row.getCell(0).setCellStyle(cellStyle);
                row.getCell(1).setCellStyle(cellStyle);
                row.getCell(2).setCellStyle(cellStyle);
                row.getCell(3).setCellStyle(cellStyle);

            }
            //画对阵图
            for (int k = 0; k < againstList.size(); k++) {
                List<String> list = againstList.get(k);
                Row row = sheet.getRow(nowRowNum++);
                for (int g = 0; g < list.size(); g++) {
                    row.getCell(5+g).setCellValue(list.get(g));
                    row.getCell(5+g).setCellStyle(cellStyle);
                }
            }

        }
        FileOutputStream fos = new FileOutputStream(pathFinished);
        workbook.write(fos);
        fos.close();




    }

    /**
     *  从赛程获取队伍数量
     * @param scheduleList
     * @return
     */
    public static int getTeamNum(List<AgainstVO> scheduleList){
        List<String> hostList = scheduleList.stream().map(AgainstVO::getAgainstHost).collect(Collectors.toList());
        List<String> guestList = scheduleList.stream().map(AgainstVO::getAgainstGuest).collect(Collectors.toList());
        hostList.addAll(guestList);
        Long sizeLong = hostList.stream().distinct().collect(Collectors.counting());
        return sizeLong.intValue();
    }
}

























